package com.huang.strategic;

import com.huang.factory.Calculator;
import com.huang.factory.CalculatorFactory;
import com.huang.strategic.v1.CashierFactory;
import com.huang.strategic.v2.CashierContext;
import org.junit.Test;

import java.math.BigDecimal;

/**
 * <p></p>
 *
 * @author aneng huang
 * @since 2020-10-27 14:56:06
 */
public class CashierMain {

    /**
     * float转换成double
     */
    private double float2Double(float f) {
        BigDecimal b = new BigDecimal(String.valueOf(f));
        return b.doubleValue();
    }

    /**
     * 简单的实现功能
     */
    @Test
    public void sumTradeTest() {
        Cashier cashier = new Cashier("Java从入门到放弃", new BigDecimal("100"), 1, 0.8f);
        Calculator calculator = new Calculator(cashier.getNum(), cashier.getUnitPrice().doubleValue());
        double result = CalculatorFactory.createOperator("*").getResult(calculator);
        calculator = new Calculator(result, float2Double(cashier.getDiscount()));
        double a = CalculatorFactory.createOperator("*").getResult(calculator);
        System.out.println(a);
    }

    /**
     * 使用工厂模式
     */
    @Test
    public void cashFactoryTest() {
        int num = 20;
        double unitPrice = 100;
        double v = CashierFactory.createCashierAccept("0").acceptCash(num * unitPrice);
        System.out.println("正常价格是：" + v);
        v = CashierFactory.createCashierAccept("1").acceptCash(num * unitPrice);
        System.out.println("打8折价格是：" + v );
        v = CashierFactory.createCashierAccept("2").acceptCash(num * unitPrice);
        System.out.println("满减价格是：" + v);
    }

    @Test
    public void strategyTest() {
        int num = 20;
        double unitPrice = 100;
        double totalPrice = num * unitPrice;
        double v = new CashierContext("0").getResult(totalPrice);
        System.out.println("正常的价格是：" + v);
        v = new CashierContext("5").getResult(totalPrice);
        System.out.println("打5折价格是：" + v );
        v = new CashierContext("4").getResult(totalPrice);
        System.out.println("打6折价格是：" + v );
        v = new CashierContext("3").getResult(totalPrice);
        System.out.println("打7折价格是：" + v );
        v = new CashierContext("1").getResult(totalPrice);
        System.out.println("打8折价格是：" + v );
        v = new CashierContext("2").getResult(totalPrice);
        System.out.println("满减的价格是：" + v);
    }
}
